import { formatResponse } from "./tools";

/**
 * 错误处理基类
 */

export class ServerError extends Error {
  constructor(public msg: string, public code: number) {
    super(msg);
  }

  // 给客户端响应错误
  toResponseJSON() {
    return formatResponse<null>(null, this.msg, this.code);
  }
}

/**
 * 文件上传错误
 */
export class UploadError extends ServerError {
  constructor(msg: string) {
    super(msg, 413);
  }
}

/**
 * 禁止访问错误
 */
export class ForbiddenError extends ServerError {
  constructor(msg: string) {
    super(msg, 401);
  }
}

/**
 * 验证错误
 */
export class ValidationError extends ServerError {
  constructor(msg: string) {
    super(msg, 406);
  }
}

/**
 * 无资源错误
 */
export class NotFoundError extends ServerError {
  constructor(msg: string) {
    super(msg, 406);
  }
}

/**
 * 未知错误
 */
export class UnknownError extends ServerError {
  constructor() {
    super("Server internal error", 500);
  }
}
